package com.gxan.web.servlet.recruitServlet;

import com.alibaba.fastjson.JSONObject;
import com.gxan.pojo.Recruit;
import com.gxan.service.RecruitService;
import com.gxan.service.impl.RecruitServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/recruit/jobPost/searchPost")
public class SearchPostServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf-8");

        Map resultMap = new HashMap<>();
        try {
            String job_name = req.getParameter("job_name");
            String job_type = req.getParameter("job_type");
            String time1 = req.getParameter("regist_time");
            String time2 = req.getParameter("deadline");

            Integer page =  Integer.parseInt(req.getParameter("page"));
            Integer limit =  Integer.parseInt(req.getParameter("limit"));

            RecruitService recruitService = new RecruitServiceImpl();
            List<Recruit> recruitList = recruitService.searchPost(job_name,job_type,time1,time2,page,limit);

            resultMap.put("code",0);
            resultMap.put("msg","success");
            resultMap.put("count",recruitService.countSearch(job_name,job_type,time1,time2));
            //resultMap.put("count",9);
            resultMap.put("data",recruitList);
            // 需要将Java对象转换为json字符串对象才能在网络中进行传输
            resp.getWriter().write(JSONObject.toJSONString(resultMap));
        } catch (Exception e){
            e.printStackTrace();
            resultMap.put("code",-1);
            resultMap.put("msg","系统开小差了，请联系管理员");
            // 需要将Java对象转换为json字符串对象才能在网络中进行传输
            resp.getWriter().write(JSONObject.toJSONString(resultMap));
        }
        resp.getWriter().flush();
        resp.getWriter().close();
    }
}
